home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
scope
/
176-200
/
scopedisk180
/
arexxtutorial
/
usn
/
usno.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1995-03-19
|
7KB
|
189 lines
/* usno.rexx Dial US Naval Observatory and capture the time */
/* written by Steve Poling as an excercize in AREXX programming 28 aug 89 */
/** additional modifications and tuneups by Jeremy Farrance Aug 29, 1989
** comments inside '/** **/' are by me. General convention used in the
** program is that lowercase is ARexx stuff and UPPERCASE commands are
** BaudBandit or DOS functions - this helps readability (IMHO). **/
/** set 'failed' to false (= 0) otherwise Rexx thinks it is 'FAILED' **/
options results ; failed = 0
/* USNO sends us time in the form of a pair of lines:
* - the first consists of an asterisk '*' on a line by itself
* - the 2nd consists of 3 groups of numbers followed by UTC in the form:
* jjjjj ddd hhmmss UTC
* jjjjj stands for the 5 least significant digits of the julian date
* ddd Number of days since Jan 1 of this year.
* hhmmss Hours Minutes Seconds of right now
* UTC Universal time (nearly same as GMT, sorta)
*
* Note that if the USNO changes their format, their phone number
* or their baud rate, this program will have to change
*/
/* this is my (sdp) first rexx program so please be gentle in criticism */
/* many times I was confronted with problems, and the only solution I could find
* was an ugly, brute force solution. I hope that some kind soul will view this
* code with compassion, and show me a better way to do some of these things.
* I've marked such passages with a tbd: as seen below
*/
/** many 'uglinesses' removed changed and modified by me, not all though **/
/* Here's one I owe JEREMY */
/** and I think I owe it to someone else but I don't remember anymore **/
if ~show('L',"rexxsupport.library") then do
if addlib('rexxsupport.library',0,-30,0) then
say 'added rexxsupport.library'
else do;
say 'support library not available'
exit 10
end
end
if exists('ram:USNO.tmp') then call delete('ram:USNO.tmp')
/** startup BaudBandit if its not already running **/
if ~showlist('P','BAUD') then do
address command /** attach us to the CLI or Shell **/
/** change the below command so that we can find BaudBandit on your system
** on my Amiga I keep BaudBandit in a directory with BB: assigned to it **/
RUN 'GT:BaudBandit'
WAITFORPORT 'BAUD'
address /** swap back to REXX **/
/** flag so we know to close BaudBandit back down later since we ran it **/
kill_bandit = 1
end
address 'BAUD' /* attach ourselves to baud bandit */
BAUD 1200 /* tell baudbandit to goto 1200 baud */
SEND 'AT\r' /** make sure BB is paying attention **/
call delay(50) /** do nothing for 1 second **/
do 5 /* make 5 attempts */
SEND '\wATDT1(202)653-0351\r' /* USNO phone number */
failed = 0
/* wait for anything while call goes thru */
WAIT '?'
DCD /* is we connected? */
if rc~=0 then leave /* leave loop as soon as connected */
else failed = 1 /** flag a failure **/
end
/** jump to done: if we failed to connect to anything **/
if failed then do
say 'Unable to make telephone connection'
signal done
end
/* if we're here, we're connected, slurp a couple of seconds worth of chars */
CAPTURE 'ram:USNO.tmp' /* start capture */
WAIT 'UTC'
WAIT 'UTC' /* get 2 seconds worth */
call time('R') /** start the elapsed timer so we can adjust our time **/
CAPTURE OFF
SEND '\Ah' /** hang up on 'em **/
/** open up the file containing the text we just captured (if its there) **/
if ~open('fh','ram:USNO.tmp','R') then do
say 'Unable to open capture file'
failed = 1
signal done
end /* if */ /** 'fh' is now our file handle **/
/** read in lines from the captured file until we get a complete USNO Time **/
do until eof('fh')
failed = 0
parse value readln('fh') with julian cal_day hr_min_sec timecode .
if timecode = 'UTC' then do
say 'USNO Time:' julian cal_day hr_min_sec timecode
parse var hr_min_sec 1 hours 3 mins 5 secs +2
hours = hours - 5 /* change to 4/5 for edt/est */
/* change to 5/6 for cdt/cst */
/* change to 6/7 for mdt/mst */
/* change to 7/8 for pdt/pst */
day = 1
if hours < 0 then do
hours = hours + 24
day = 0
end
/** now we get the elapsed time to adjust for the lost seconds **/
secs = secs + time('E') % 1 + 1
if secs > 59 then do
mins = mins + 1
secs = secs - 60
if mins > 59 then do
hours = hours + 1
mins = mins - 60
if hours > 23 then do
say 'Please try this again, 1 second before midnight is a pain.'
failed = 1
signal done
end
end
end
address command 'date 'hours':'mins':'secs
say 'Setting time to 'hours':'mins':'secs
/* calculate dd-mmm-yy and tell amigados what day it is */
daymonthyear = fromjulian(julian,day)
address command 'date 'daymonthyear
say 'Setting day to 'daymonthyear
/* this is an appropriate time to update your hardware clock */
/** address command 'setclock save' **/
/* address command 'settime' */
failed = 0
leave
end
failed = 1
end
done:
call close('fh')
if exists('ram:USNO.tmp') then call delete('ram:USNO.tmp')
address command 'DATE'
say 'good night Dick'
if kill_bandit == 1 then 'SEND \AQ'
if failed then do
say 'ERROR - We failed miserably'
exit 20
end
exit 0
/* many thanx and a tip of the hat to Jeremy for moving this code to a
* subroutine. If you like julian date conversion stuff, bug me for the
* my upcoming release of a whole raft of astronomical programs shamelessly
* lifted from Jean Meeus' book. Steve Poling
*/
fromjulian: procedure /* Compute dd-mmm-yy from julian */
arg julian,day
julian = julian + day /* handle midnight wrapround */
julian = julian + 2400000 /* usno truncates 2 ms digits */
/* assume they are 24 */
/* after 2150, this will break */
/* contact me and I'll send you an update */
/* See pg 26 "Astronomical Formulae for Calculators", by Jean Meeus
* 4th ed. Willmann-Bell, Richmond VA 1988. */
/* Certain simplifying assumptions are made, mostly that you'll not
* be running this program in the past. Also, fractional dates are
* ignored. */
alpha = (julian-1867216.25) % 36524.25
a = julian + 1 + alpha - (alpha%4) /* these var names are Meeus' */
b = a + 1524
c = (b - 122.1) % 365.25
d = 365.25*c; d = d % 1 /* evade AREXX arith bug */
e = (b - d) % 30.6001
day = (b - d - ((e*30.6001)%1)) % 1
if e < 13.5 then mo = e - 1
else mo = e - 13
if mo > 2.5 then year = c - 4716
else year = c - 4715
year = year // 100
return day'-'substr('JanFebMarAprMayJunJulAugSepOctNovDec',(mo-1)*3+1,3)'-'year
/** end of fromjulian() **/